﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.MacroEngines;
using System.Xml;
using umbraco.NodeFactory;

namespace DAMP.RazorModel
{
    public class Model : List<DAMP_Item>
    {
        private DynamicNodeList _dynamicNodeList;
        private DynamicXml _xml;

        private readonly string _propertyData;
        private readonly bool _isXml;

        /// <summary>
        /// Return if DAMP is stored as xml or csv. If the PropertyData is empty it will always return false.
        /// So if you store the PropertyData as xml, but no media items have been selected it will return false.
        /// </summary>
        public bool IsXml
        {
            get
            {
                return _isXml;
            }
        }

        /// <summary>
        /// Return the data the way it's stored in Umbraco.
        /// </summary>
        public string PropertyData
        {
            get
            {
                return _propertyData;
            }
        }

        /// <summary>
        /// Returns all DAMP media items as a DynamicNodeList.
        /// </summary>
        public DynamicNodeList DynamicMediaList
        {
            get
            {
                if (_dynamicNodeList == null)
                {
                    //Thought that DynamicMedia and DynamicMediaList should be used, but in the core it also uses DynamicNode.
                    List<DynamicNode> nodes = new List<DynamicNode>();

                    if (IsXml)
                    {   
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(PropertyData);

                        foreach (XmlNode item in doc.SelectNodes("//mediaItem/*"))
                        {
                            DynamicMedia media = new DynamicMedia(Convert.ToInt32(item.Attributes["id"].Value));
                            nodes.Add(media);
                        }
                    }
                    else
                    {
                        foreach (string id in PropertyData.Split(','))
                        {
                            DynamicMedia media = new DynamicMedia(Convert.ToInt32(id));
                            nodes.Add(media);
                        } 
                    }

                    _dynamicNodeList = new DynamicNodeList(nodes);
                }

                return _dynamicNodeList;
            }
        }

        /// <summary>
        /// Return the first item from the List<DAMP_Item>.
        /// </summary>
        public DAMP_Item First
        {
            get
            {
                return this.First();
            }
        }

        /// <summary>
        /// Check if this list has any media items.
        /// </summary>
        public bool Any
        {
            get
            {
                return this.Any();
            }
        }

        /// <summary>
        /// Use the Skip extension method on this list.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public DAMP_List Skip(int count)
        {
            return new DAMP_List(Enumerable.Skip(this, count));
        }

        /// <summary>
        /// Use the Take extension method on this list.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public DAMP_List Take(int count)
        {
            return new DAMP_List(Enumerable.Take(this, count));
        }

        /// <summary>
        /// Return the DAMP data as DynamicXml.
        /// </summary>
        public DynamicXml Xml
        {
            get
            {
                if (_xml == null)
                {
                    _xml = new DynamicXml(PropertyData);
                }

                return _xml;
            }
        }

        public Model(string propertyData)
        {
            _propertyData = propertyData;
            _isXml = Helper.CouldItBeXml(PropertyData);

            if (!string.IsNullOrEmpty(PropertyData))
            {
                if (IsXml)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(PropertyData);

                    foreach (XmlNode item in doc.SelectNodes("descendant::mediaItem/*"))
                    {
                        this.Add(new DAMP_Item(item));
                    }
                }
                else
                {
                    foreach (string id in PropertyData.Split(','))
                    {
                        this.Add(new DAMP_Item(Convert.ToInt32(id)));
                    }
                }
            }
        }
    }
}
